package com.tianyuan.dplrf.ui.activity;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.widget.NestedScrollView;


import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.base.FragmentPagerAdapter;
import com.hjq.base.action.AnimAction;
import com.hjq.widget.layout.NoScrollViewPager;
import com.tianyuan.dplrf.R;
import com.tianyuan.dplrf.app.AppActivity;
import com.tianyuan.dplrf.app.AppFragment;
import com.tianyuan.dplrf.ui.bean.Ncjt_tianqibean;
import com.tianyuan.dplrf.ui.fragment.FragmentLandBase;
import com.tianyuan.dplrf.ui.fragment.FragmentN1;
import com.tianyuan.dplrf.ui.fragment.FragmentN2;
import com.tianyuan.dplrf.ui.fragment.FragmentN3;
import com.tianyuan.dplrf.ui.fragment.FragmentN4;
import com.tianyuan.dplrf.ui.popup.TianqiPop;
import com.tianyuan.dplrf.ui.utiils.Uiutils;
import com.xuexiang.xutil.display.DensityUtils;
import com.xuexiang.xutil.display.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public class Minenc_jtActivity extends AppActivity implements View.OnClickListener, AnimAction {
    private AppCompatImageView img_ncbackground;
    private ImageView img_land_back;
    private TextView tv_title;
    private AppCompatImageView img_wifi;
    private AppCompatImageView img_tianqi;
    private AppCompatTextView tv_tianqi;
    private AppCompatButton btn_feiliao;
    private AppCompatButton btn_zhongmiao;
    private AppCompatButton btn_shangpingchushou;
    private AppCompatButton btn_shangpinggl;
    private FrameLayout frame_content;
    private LinearLayout linear_toptianqi;
    private NestedScrollView nestscroll;
    private NoScrollViewPager view_pager;
    private AppCompatImageView img_close_pop;
    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;
    private AppCompatImageView img_zhongjs;
    private AppCompatImageView img_shangqfl;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_minenc_jt;
    }

    @Override
    protected void initView() {

        img_ncbackground = findViewById(R.id.img_ncbackground);
        img_land_back = findViewById(R.id.img_land_back);
        tv_title = findViewById(R.id.tv_title);
        img_wifi = findViewById(R.id.img_wifi);
        img_tianqi = findViewById(R.id.img_tianqi);
        tv_tianqi = findViewById(R.id.tv_tianqi);
        btn_feiliao = findViewById(R.id.btn_feiliao);
        btn_zhongmiao = findViewById(R.id.btn_zhongmiao);
        btn_shangpingchushou = findViewById(R.id.btn_shangpingchushou);
        btn_shangpinggl = findViewById(R.id.btn_shangpinggl);
        img_zhongjs = findViewById(R.id.img_zhongjs);
        img_shangqfl = findViewById(R.id.img_shangqfl);
        img_land_back.setOnClickListener(this);
        img_tianqi.setOnClickListener(this);
        tv_tianqi.setOnClickListener(this);
        btn_feiliao.setOnClickListener(this);
        btn_zhongmiao.setOnClickListener(this);
        btn_shangpingchushou.setOnClickListener(this);
        btn_shangpinggl.setOnClickListener(this);
        img_zhongjs.setOnClickListener(this);
        img_shangqfl.setOnClickListener(this);

        tv_title.setVisibility(View.GONE);
        frame_content = findViewById(R.id.frame_content);

        Log.e(getTag(), "initView: getScreenWidth" + ScreenUtils.getScreenWidth());//2376
        Log.e(getTag(), "initView: getRealDisplayMetrics" + ScreenUtils.getRealDisplayMetrics().widthPixels);


        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(DensityUtils.getScreenWidth() / 3, ViewGroup.LayoutParams.MATCH_PARENT);
        frame_content.setLayoutParams(params);
        frame_content.setX(DensityUtils.getScreenWidth() * 2 / 3 + Uiutils.getStatusBarHeight(getApplicationContext()));
        frame_content.setVisibility(View.GONE);


        linear_toptianqi = findViewById(R.id.linear_toptianqi);
        nestscroll = findViewById(R.id.nestscroll);
        view_pager = findViewById(R.id.view_pager);
        img_close_pop = findViewById(R.id.img_close_pop);
        img_close_pop.setOnClickListener(this);
    }

    @Override
    protected void initData() {

        mPagerAdapter = new FragmentPagerAdapter<>(this);
        mPagerAdapter.addFragment(FragmentN1.newInstance());
        mPagerAdapter.addFragment(FragmentN2.newInstance());
        mPagerAdapter.addFragment(FragmentN3.newInstance());
        mPagerAdapter.addFragment(FragmentN4.newInstance());
        view_pager.setAdapter(mPagerAdapter);

        view_pager.setCurrentItem(0);

        initTianqidata();
        initTurdata();
        initSqfldata();
    }



    private void switchFragment(int fragmentIndex) {
        if (fragmentIndex == -1) {
            return;
        }

        switch (fragmentIndex) {
            case 0:
            case 1:
            case 2:
            case 3:
                view_pager.setCurrentItem(fragmentIndex);
                linear_toptianqi.setVisibility(View.GONE);
                nestscroll.setVisibility(View.GONE);
                frame_content.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    /**
     * 右侧弹框隐藏
     */
    private void hidePop() {
        frame_content.setVisibility(View.GONE);
        linear_toptianqi.setVisibility(View.VISIBLE);
        nestscroll.setVisibility(View.VISIBLE);
    }

    private TianqiPop.Builder mypop;
    private TianqiPop.Builder popZjs;
    private TianqiPop.Builder popSqfl;
    private List<Ncjt_tianqibean> tianqilist;
    private List<Ncjt_tianqibean> turList;
    private List<Ncjt_tianqibean> tursqflList;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_land_back:

                finish();
                break;
            case R.id.img_tianqi:
                if (mypop == null) {
                    mypop = new TianqiPop.Builder(this);
                    mypop.setDefault(linear_toptianqi.getWidth(),ViewGroup.LayoutParams.WRAP_CONTENT,R.style.ScaleAnimStyle);
                    mypop.setData(tianqilist);
                    mypop.setmAdapter();
                    mypop.create().showAsDropDown(linear_toptianqi, Gravity.TOP, 0, linear_toptianqi.getHeight() + DensityUtils.dip2px(8));
                } else {
                    if (mypop.isCreated()) {
                        mypop.showAsDropDown(linear_toptianqi);
                    }
                }
                break;
            case R.id.img_zhongjs:
                if (popZjs == null) {
                    popZjs = new TianqiPop.Builder(this);
                    popZjs.setDefault(linear_toptianqi.getWidth(),ViewGroup.LayoutParams.WRAP_CONTENT,R.style.ScaleAnimStyle);
                    popZjs.setData(turList);
                    popZjs.setmAdapter();
                    popZjs.setTitleMessage("土壤重金属监测","2小时前更新");
                    popZjs.create().showAsDropDown(linear_toptianqi, Gravity.TOP, 0, linear_toptianqi.getHeight() + DensityUtils.dip2px(8));
                } else {
                    if (popZjs.isCreated()) {
                        popZjs.showAsDropDown(linear_toptianqi);
                    }
                }
                break;
            case R.id.img_shangqfl:
                if (popSqfl == null) {
                    popSqfl = new TianqiPop.Builder(this);
                    popSqfl.setDefault(linear_toptianqi.getWidth(),ViewGroup.LayoutParams.WRAP_CONTENT,R.style.ScaleAnimStyle);
                    popSqfl.setData(tursqflList);
                    popSqfl.setmAdapter();
                    popSqfl.setTitleMessage("土壤墒情肥力监测","3小时前更新");
                    popSqfl.create().showAsDropDown(linear_toptianqi, Gravity.TOP, 0, linear_toptianqi.getHeight() + DensityUtils.dip2px(8));
                } else {
                    if (popSqfl.isCreated()) {
                        popSqfl.showAsDropDown(linear_toptianqi);
                    }
                }
                break;
            case R.id.btn_feiliao:
                switchFragment(0);
                break;
            case R.id.btn_zhongmiao:
                switchFragment(1);
                break;
            case R.id.btn_shangpingchushou:
//                switchFragment(2);
                break;
            case R.id.btn_shangpinggl:
                switchFragment(3);
                break;
            case R.id.img_close_pop:
                //下发关闭事件
                ((FragmentLandBase) mPagerAdapter.getItem(view_pager.getCurrentItem())).onBackClick();

                break;
        }
    }

    private void initSqfldata() {
        tursqflList = new ArrayList<>();
        addSqitem("土壤PH","无","6.85","");
        addSqitem("氮含量","mg/kg","132","");
        addSqitem("磷含量","mg/kg","23","");
        addSqitem("钾含量","mg/kg","147","");
    }

    private void initTurdata() {
        turList = new ArrayList<>();
        addZhongjsitem("土壤铜离子","ppm","0","");
        addZhongjsitem("土壤铅离子","ppm","0","");
        addZhongjsitem("土壤镉离子","ppm","0.7","");
        addZhongjsitem("水质PH","无","7","");

    }

    private void initTianqidata() {
        tianqilist = new ArrayList<>();
        addTiqiitem("空气温度", "°C", "16°C", "");
        addTiqiitem("空气湿度", "%RH", "25%", "");
        addTiqiitem("光照指数", "0-15", "4", "");
        addTiqiitem("PM2.5", "微克/m³", "153", "轻度污染");
        addTiqiitem("风速", "m/s", "3m/s", "");
        addTiqiitem("大气压", "百帕（hPa）", "1016.2hPa", "");
    }

    private void addTiqiitem(String var1, String var2, String s1, String s2) {
        Ncjt_tianqibean bean = new Ncjt_tianqibean();
        bean.setName(var1);
        bean.setDanweil(var2);
        bean.setData(s1);
        bean.setCatz(s2);
        tianqilist.add(bean);
    }

    private void addZhongjsitem(String var1, String var2, String s1, String s2) {
        Ncjt_tianqibean bean = new Ncjt_tianqibean();
        bean.setName(var1);
        bean.setDanweil(var2);
        bean.setData(s1);
        bean.setCatz(s2);
        turList.add(bean);
    }

    private void addSqitem(String var1, String var2, String s1, String s2) {
        Ncjt_tianqibean bean = new Ncjt_tianqibean();
        bean.setName(var1);
        bean.setDanweil(var2);
        bean.setData(s1);
        bean.setCatz(s2);
        tursqflList.add(bean);
    }

    public void onPopfinish() {
        hidePop();
    }


}